package com.nexen.merchant.serviceRequest.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.nexen.common.utils.DateUtils;
import com.nexen.common.utils.StringUtils;
import com.nexen.framework.manager.AsyncManager;
import com.nexen.framework.manager.factory.AsyncFactory;
import com.nexen.merchant.barcode.mapper.NexenBarcodeMapper;
import com.nexen.merchant.serviceRequest.domain.NexenServicerequest;
import com.nexen.merchant.serviceRequest.mapper.NexenServicerequestMapper;
import com.nexen.merchant.serviceRequest.service.INexenServicerequestService;
import com.nexen.merchant.warrantyCard.domain.NexenWarrantycard;
import com.nexen.merchant.warrantyCard.mapper.NexenWarrantycardMapper;
import com.nexen.system.domain.SysOperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 换胎申请Service业务层处理
 * 
 * @author xiao
 * @date 2024-08-03
 */
@Service
public class NexenServicerequestServiceImpl implements INexenServicerequestService
{

    @Value("${mini-api}")
    private String miniApiUrl;

    @Autowired
    private NexenWarrantycardMapper nexenWarrantycardMapper;
    @Autowired
    private NexenServicerequestMapper nexenServicerequestMapper;
    @Autowired
    private NexenBarcodeMapper nexenBarcodeMapper;

    /**
     * 查询换胎申请
     * 
     * @param Id 换胎申请主键
     * @return 换胎申请
     */
    @Override
    public NexenServicerequest selectNexenServicerequestById(Long Id)
    {
        NexenServicerequest nexenServicerequest = nexenServicerequestMapper.selectNexenServicerequestById(Id);
        String newTireCode = nexenServicerequest.getNewTireCode();
        List<String> newTireCodeList= new ArrayList<>();
        if(StringUtils.isNotBlank(newTireCode)){
            if(newTireCode.indexOf("|") == -1) {
                nexenServicerequest.setTireCode1Info(nexenBarcodeMapper.selectNexenBarcodeByCode(newTireCode));
            }else {
                newTireCodeList = Arrays.asList(newTireCode.split("\\|"));
                newTireCode.split("|");
                for (int i = 0; i < newTireCodeList.size(); i++) {
                    String tireCode = newTireCodeList.get(i);
                    if (StringUtils.isNotBlank(tireCode) && i==0) {
                        nexenServicerequest.setTireCode1Info(nexenBarcodeMapper.selectNexenBarcodeByCode(tireCode));
                    }
                    if (StringUtils.isNotBlank(tireCode)  && i==1) {
                        nexenServicerequest.setTireCode2Info(nexenBarcodeMapper.selectNexenBarcodeByCode(tireCode));
                    }
                    if (StringUtils.isNotBlank(tireCode)  && i==2) {
                        nexenServicerequest.setTireCode3Info(nexenBarcodeMapper.selectNexenBarcodeByCode(tireCode));
                    }
                    if (StringUtils.isNotBlank(tireCode)  && i==3) {
                        nexenServicerequest.setTireCode4Info(nexenBarcodeMapper.selectNexenBarcodeByCode(tireCode));
                    }
                }
            }
        }

        return nexenServicerequest;
    }

    /**
     * 查询换胎申请列表
     * 
     * @param nexenServicerequest 换胎申请
     * @return 换胎申请
     */
    @Override
    public List<NexenServicerequest> selectNexenServicerequestList(NexenServicerequest nexenServicerequest)
    {
        return nexenServicerequestMapper.selectNexenServicerequestList(nexenServicerequest);
    }

    /**
     * 新增换胎申请
     * 
     * @param nexenServicerequest 换胎申请
     * @return 结果
     */
    @Override
    public int insertNexenServicerequest(NexenServicerequest nexenServicerequest)
    {
        nexenServicerequest.setCreateTime(DateUtils.getNowDate());
        return nexenServicerequestMapper.insertNexenServicerequest(nexenServicerequest);
    }

    /**
     * 修改换胎申请
     * 
     * @param nexenServicerequest 换胎申请
     * @return 结果
     */
    @Override
    public int updateNexenServicerequest(NexenServicerequest nexenServicerequest)
    {
        nexenServicerequest.setUpdateTime(DateUtils.getNowDate());
        return nexenServicerequestMapper.updateNexenServicerequest(nexenServicerequest);
    }

    /**
     * 批量删除换胎申请
     * 
     * @param Ids 需要删除的换胎申请主键
     * @return 结果
     */
    @Override
    public int deleteNexenServicerequestByIds(Long[] Ids)
    {
        return nexenServicerequestMapper.deleteNexenServicerequestByIds(Ids);
    }

    /**
     * 删除换胎申请信息
     * 
     * @param Id 换胎申请主键
     * @return 结果
     */
    @Override
    public int deleteNexenServicerequestById(Long Id)
    {
        return nexenServicerequestMapper.deleteNexenServicerequestById(Id);
    }

    @Override
    public int updateAuditNexenServicerequest(NexenServicerequest nexenServicerequest) {
        int i = nexenServicerequestMapper.updateAuditStatus(nexenServicerequest);
        if (i==1) {
            Long id = nexenServicerequest.getId();
            Long status = nexenServicerequest.getStatus();
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("id", id);
            jsonBody.put("type","service" );
            // 发送POST请求，并设置请求的URL、请求头和请求体
            String result = HttpUtil.createPost(miniApiUrl)
                    .header("Content-Type", "application/json; charset=utf-8")
                    .body(jsonBody.toString())
                    .execute()
                    .body();
            // 异步记录日志
            SysOperLog sysOperLog = new SysOperLog();
            sysOperLog.setTitle("换胎申请服务消息");
            sysOperLog.setBusinessType(2);
            sysOperLog.setOperatorType(1);
            sysOperLog.setRequestMethod("POST");
            sysOperLog.setOperUrl(miniApiUrl);
            sysOperLog.setOperParam(jsonBody.toString());
            sysOperLog.setOperName(nexenServicerequest.getAuditBy());
            sysOperLog.setJsonResult(result);
            sysOperLog.setOperIp("内网IP");
            sysOperLog.setStatus(0);
            sysOperLog.setOperTime(DateUtil.date());
            AsyncManager.me().execute(AsyncFactory.recordOper(sysOperLog));
            nexenServicerequest.setUpdateTime(DateUtils.getNowDate());

            if(status == 3) {
                NexenServicerequest nexenServicerequestInfo = nexenServicerequestMapper.selectNexenServicerequestById(id);
                NexenWarrantycard nexenWarrantycard = new NexenWarrantycard();
                nexenWarrantycard.setId(nexenServicerequestInfo.getCardId());
                nexenWarrantycard.setStatus(2L);
                nexenWarrantycardMapper.updateAuditStatus(nexenWarrantycard);
            }
        }
        return i;
    }

    @Override
    public List<NexenServicerequest> selectAllList(NexenServicerequest nexenServicerequest) {
        return nexenServicerequestMapper.selectAllList(nexenServicerequest);
    }

    @Override
    public int updateNexenServicerequestSendSMSByIds(List<Long> ids) {
       return nexenServicerequestMapper.updateNexenServicerequestSendSMSByIds(ids);
    }

}
